#include <dl.h>
using namespace dl;

#include <system/dl_windows.h>


//#define IS_TEST

#ifdef IS_TEST
#include "out.h"
#endif

int main()
{
	Engine::Init();

	log_msg("==============================dl 配置生成代码工具=======================");


#ifndef IS_TEST
	json j = Json::Parse("config.json");
	if (j.is_null())
	{
		log_msg0("config.json读取失败！请如下配置：");
		log_info0(
			R"({
	"author":"作者（注释）",
	"filename":"文件名（注释）",
	"log_function": "日志打印函数",
	"register_lua": "是否导出lua（依赖luabridge3）",
	"path_in": "输入配置",
	"path_out_code": "输出代码文件路径",
	"path_out_json": "输出配置文件路径",
	"type_replace":{
		"uchar":"palette",
		"vector<uchar>":"vector<palette>",
	}
}
})");
		system("pause");
		return -1;
	}

	std::string path_in;
	std::string path_out_code;
	std::string path_out_json;
	Json::Get(j, "path_in", path_in);
	Json::Get(j, "path_out_code", path_out_code);
	Json::Get(j, "path_out_json", path_out_json);

	JsonConfig config;
	if (config.Load(path_in))
	{
		JsonConfig::GenerateParam param;
		Json::Get(j, "author", param._author);
		Json::Get(j, "filename", param._filename);
		Json::Get(j, "log_function", param._logFunction);
		Json::Get(j, "register_lua", param._bRegisterLua);


		std::unordered_map<std::string, std::string> map_type;
		Json::Get(j, "type_replace", map_type);

		if(File::SaveString(config.GenerateCode(param), path_out_code))
			Win32::OpenUrl(File::GetFilePath(File::GetAbsolute(path_out_code)));
		if (File::SaveString(config.GenerateConfigUser(map_type), path_out_json))
			Win32::OpenUrl(File::GetFilePath(File::GetAbsolute(path_out_json)));
	}

	

	log_msg0("恭嘿你，完成！");
#else
	json j = Json::Parse("out.json");
	tagConfigTest config;
	config.Load(j);
	config.RegisterLua(g_lua->GetL(), "config");

	g_lua->DoFile("test.lua");
#endif
	Engine::Release();

	system("pause");
	return 0;
}
